#include "mcNode.h"
#include "head.h"

using namespace std;

int mcNode::mcNode_num_static = 0;
SgBlackWhite mcNode::Color = -1;
SgVectorOf<mcNode> mcNode::possible_next_moves = SgVectorOf<mcNode> ();

//list<mcNode> mcNode::possible_next_moves=new list<mcNode>();

mcNode::mcNode() {
	//do nothing, just for test
}

mcNode::mcNode(mcNode* mcPointer, Move* move) {

	this->father = mcPointer;
	if (this->father == NULL) {
		//TODO   this->myColor = board->toPlay();
		depth = 1;
		move = NULL;
	} else {
		depth = this->father->depth + 1;
		this->curMove = *move;
	}
	my_game_win = false;
	total_win_game = 0;

	mcNode_num_static = mcNode_num_static + 1;
	mcNode_num = mcNode_num_static;
	print();
}

void mcNode::simulate(const RegionBoard& rb) {
	cout << "mcNode:    " << mcNode_num << "    simulate";
	//my_game=true;
	//my_game_win=true;
	//TODO   	RegionBoard temprb(this->regBoard->getBoard());
	//TODO   	Board tempboard = temprb.getBoard();

	MoveList mlist = this->getMoveList();
	Board tempboard = rb.getBoard();
	RegionBoard temprb(tempboard);

	for (int i = 0; i < mlist.Length(); i++) {
		tempboard.play(mlist[i]);
		temprb.onExecMove(mlist[i]);
	}

	srand((unsigned) time(0));
	int temp;
	SgBlackWhite bw;
	for (int i = 0; i < 100; ++i) {

		temp = rand() % SG_MAX_SIZE * SG_MAX_SIZE + 1;
		temp = tempboard.getEmptyPoint(temp);
		cout << temp << "  ";
		SgPoint sp(temp);
		bw = tempboard.toPlay();

		if (tempboard.isLegal(sp, bw)) {
			Move move(bw, sp);
			tempboard.play(move);
			temprb.onExecMove(move);
		}
	}
	SgBlackWhite who = temprb.whoWins();

	if (this->father != NULL) {
		if (who == this->Color)
			father->from_child(true);
		else
			father->from_child(false);
	}

	//here to extends the node.
	//b->isLegal();
	//b->whoWins();
	//b->play(Move m);
	//board.play(move);
	//regionBoard.onExecMove(move);


	//father->from_child(my_game_win*);
}

void mcNode::from_child(bool bolvalue) {
	if (bolvalue == true) {
		//TODO   		++this->total_done_evaluaction;
		++this->total_win_game;
	} else if (bolvalue == false) {
		//TODO   		++this->total_done_evaluaction;
	} else {
		//TODO   		++this->total_done_evaluaction;
		SgDebug() << "ERROR: mcNode::from_child\n";
	}

	if (this->father != NULL) {
		this->father->from_child(bolvalue);
	}
}

MoveList mcNode::getMoveList() {
	if (this->depth == 1) {
		SgDebug() << "\n fathe==null   :" << this->mcNode_num << "\n";

		MoveList m;
		return m;
	} else {
		SgDebug() << "\n father!+null   this:" << this->mcNode_num << "\n";
		SgDebug() << "\n father!+null   fa:" << this->father->mcNode_num
				<< "\n";
		SG_ASSERT(this != this->father);
		//SG_ASSERT(mcNode_num != father->mcNode_num);
		MoveList m = this->father->getMoveList();

		m.Include(this->curMove);
		return m;
	}
}

void mcNode::extends_this(Move* move) {
	mcNode *a = new mcNode(this, move);
	a->print();
	//	SG_ASSERT(a->mcNode_num == a->father->mcNode_num);
	this->moveList.Include(*move);
	this->sons_of_this_node.Include(a);
	this->possible_next_moves.Include(a);

}

